package com.comtop.topcloud.device.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class ThreadPoolConfig {

    @Bean(name = "customThreadPool")
    public Executor customThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数（线程池维护线程的最少数量）
        executor.setCorePoolSize(10);
        // 最大线程数（线程池维护线程的最大数量）
        executor.setMaxPoolSize(50);
        // 队列容量（当核心线程数已满时，新任务会进入队列）
        executor.setQueueCapacity(200);
        // 线程活跃时间（当线程数大于核心线程数时，多余的空闲线程的最大存活时间）
        executor.setKeepAliveSeconds(60);
        // 线程名称前缀
        executor.setThreadNamePrefix("CustomExecutor-");
        // 任务拒绝策略
        executor.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化线程池
        executor.initialize();
        return executor;
    }
}
